//
//  leftContentView.m
//  Life assistant
//
//  Created by HYMac on 16/2/26.
//  Copyright © 2016年 HY. All rights reserved.
//

#import "leftContentView.h"
#import "UIButton+move.h"
@implementation leftContentView
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame])
    {
        self.backgroundColor=[UIColor clearColor];
       [self createBtn];
        
   
  //    NSTimer *createTimer=[NSTimer scheduledTimerWithTimeInterval:6.0 target:self selector:@selector(createBtn) userInfo:nil repeats:YES];
    //   NSTimer *refushTimer=[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(refush) userInfo:nil repeats:YES];
      //  [[NSRunLoop currentRunLoop]addTimer:timer forMode:NSRunLoopCommonModes];
    }
    return self;
}
-(void)refush
{
    [self setNeedsDisplay];
   
}
-(void)createBtn
{
    for (int i=0; i<5; i++)
    {
       // UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 00)];
        CGFloat btnX=arc4random_uniform(self.frame.size.width-39) ;
        CGFloat btnY=arc4random_uniform(self.frame.size.height-24);
   
        CGFloat btnW=40;
        CGFloat btnH=25;
        UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
        //NSLog(@"%@",[UIFont familyNames]);
        NSLog(@"%p",btn);
        btn.titleLabel.font=[UIFont fontWithName:@"Iowan Old Style" size:10];
        btn.backgroundColor=[UIColor redColor];
        btn.btnTag=arc4random_uniform(7);
        btn.color=arc4random_uniform(3);
        btn.xV=arc4random_uniform(20)+1;
        
       NSLog(@"%f",btn.xV);
        btn.yV=arc4random_uniform(20)+1;
        btn.derectionXY=arc4random_uniform(2)+1;
        switch (btn.color)
        {
            case redBtn:
                btn.backgroundColor=[UIColor redColor];
                break;
            case blueBtn:
                btn.backgroundColor=[UIColor blueColor];
                break;
            case greenBtn:
                btn.backgroundColor=[UIColor orangeColor];
                break;
            default:
                break;
        }
       
        
        switch (btn.btnTag)
        {
            case 0:
                [btn setTitle:@"表白" forState:UIControlStateNormal];
                break;
            case 1:
                 [btn setTitle:@"讨好" forState:UIControlStateNormal];
                break;
            case 2:
                [btn setTitle:@"唠嗑" forState:UIControlStateNormal];
                break;
            case 3:
                [btn setTitle:@"大爱" forState:UIControlStateNormal];
            case 4:
                [btn setTitle:@"宠物" forState:UIControlStateNormal];
                break;
            case 5:
                [btn setTitle:@"朋友阶段" forState:UIControlStateNormal];
                break;
            case 6:
                [btn setTitle:@"打骂阶段" forState:UIControlStateNormal];
                break;
            default:
                break;
        }

        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
       
        [self addSubview:btn];
        
    }
}
- (void)btnClick:(UIButton *)btn
{
    NSLog(@"%ld",(long)btn.xV);
}
-(void)drawRect:(CGRect)rect
{
   // NSLog(@"%@",self.subviews);
    for (UIButton *btn in self.subviews)
    {
        NSLog(@"%f",btn.xV);
        // NSLog(@"%p",btn);
     
//        if (btn.frame.origin.x<0||btn.frame.origin.x>self.frame.size.width||btn.frame.origin.y>self.frame.size.height)
//        {
//          [btn removeFromSuperview];
//        }
//        else
//        {
//            if (btn.derection==0)
//            {
//                CGRect temp=btn.frame;
//                temp.origin.x-=btn.xV;
//                temp.origin.y+=3;
//                btn.frame=temp;
//            }
//            else
//            {
//                CGRect temp=btn.frame;
//                temp.origin.x+=btn.xV;
//                temp.origin.y+=3;
//                btn.frame=temp;
//            }
//        }
   //     NSLog(@"%d",btn.derection);
    
        
    }

}
@end
